package com.ag.contactTree;

import java.util.List;

import org.apache.log4j.Logger;
import org.junit.Test;

import com.ag.bean.TbDeptType;
import com.ag.zhh.dao.DeptDAO;
import com.ag.zhh.dao.UserDeptDAO;
import com.ag.zhh.vo.Dept;
import com.ag.zhh.vo.UserDept;

public class CoordinatTree extends AbstractTree {
	private static Logger logger = Logger.getLogger(CoordinatTree.class);
	
	private volatile static CoordinatTree instance = null;
	
	
	private static Object lock = new Object();
	
	private CoordinatTree() {

	}

	public static CoordinatTree getInstance(UserDeptDAO userdeptDao) {
		if (instance == null) {
			synchronized (lock) {
				if (instance == null) {
					instance = new CoordinatTree();
					logger.info("CoordinatTree-----------reloading");
					instance.reloadCoordinatDept(userdeptDao);
				}
			}
		}
		return instance;
	}

	public synchronized void reloadCoordinatDept(UserDeptDAO userdeptDao) {
		// TODO Auto-generated method stub
		List nodes;
		try {
			TbDeptType deptType = new TbDeptType();
			deptType.setDtId("0001");
			deptType.setDtName("协同部门");
			// nodes = deptDao.findByDeptType(new TbDeptType("0002", "协调部门"));
			nodes = userdeptDao.findByDeptType(deptType);
			super.reload(nodes);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	protected TreeNode transform(Object info) {
		// TODO Auto-generated method stub
		UserDept dept = (UserDept) info;
		TreeNode node = new TreeNode();
		node.setNodeId(dept.getUdId());
		if (dept.getTbUserDept() != null) {
			node.setParentId(dept.getTbUserDept().getUdId());
		}
		node.setBindData(dept);
		return node;
	}

}
